﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.DAL;
using SalesSys.DAL.Base;
using System.ComponentModel.DataAnnotations;

namespace SalesSys.DAL.Models
{
    /*
     * 客户相关实体
     */
    /// <summary>
    /// 客户
    /// </summary>
    public class Customer : DbEntityBase<Guid>, ILogicalDelEntity
    {
        public Customer()
        {
            CreateTime = DateTime.Now;
        }
        /// <summary>
        /// 是否个人户
        /// </summary>
        public bool IsPersonal { get; set; }

        /// <summary>
        /// 客户简称
        /// </summary>
        public string ShortName { get; set; }

        /// <summary>
        /// 客户名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 地区
        /// </summary>
        public virtual District District { get; set; }

        /// <summary>
        /// 详细地址
        /// </summary>
        public string Address { get; set; }
       

        /// <summary>
        /// 联络人
        /// </summary>
        public string Contacts { get; set; }
        /// <summary>
        /// 联络人职务
        /// </summary>
        public string ContactPosition { get; set; }

        /// <summary>
        /// 联络人电话
        /// </summary>
        public string ContactPhone { get; set; }

        /// <summary>
        /// 财务
        /// </summary>
        public string Accountant { get; set; }

        /// <summary>
        /// 财务电话
        /// </summary>
        public string AccountantPhone { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 创建人-客户经理
        /// </summary>
        public virtual Employee ClientManager { get; set; }

        /// <summary>
        /// 户头列表
        /// </summary>
        public virtual ICollection<Account> Accounts { get; set; }
        /// <summary>
        /// 普通客户联系人集合
        /// </summary>
        public virtual ICollection<CustomerContact> CustomerContacts { get; set; }
        /// <summary>
        /// 财务客户联系人集合
        /// </summary>
        public virtual ICollection<AccountantContact> AccountantContacts { get; set; }
        /// <summary>
        /// 唯一身份识别码，身份证
        /// </summary>
        public string IDCode { get; set; }

        /// <summary>
        /// 信用代码，企业用户
        /// </summary>
        public string CreditID { get; set; }

        /// <summary>
        /// 审批状态
        /// </summary>
        //   public bool IsConfirm { get; set; }
        public ApprovalState ApprovalState { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }

        /// <summary>
        /// 历史记录
        /// </summary>
        public string Record { get; set; }

        /// <summary>
        /// 信用评分
        /// </summary>
        public int CreditScore { get; set; }

        /// <summary>
        /// 计算评级
        /// </summary>
        public bool CreditGrade()
        {
            //todo..根据评级算法计算是否需要先款
            return CreditScore >= 80;//A级别85分，B级80分，C及75分
        }

        /// <summary>
        /// 计算评级
        /// </summary>
        public void AddRecord(string record)
        {
            this.Record = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "]  " + record+"<br/>" + this.Record;
        }

        public class Mapper : SalMapper<Customer, Guid>
        {
            public Mapper()
            {
                Property(p => p.ShortName).HasMaxLength(50);
                Property(p => p.Name).HasMaxLength(255);
                HasMany(p => p.Accounts);
                Property(p => p.AccountantPhone).HasMaxLength(50);
                Property(p => p.Address).HasMaxLength(255);
                Property(p => p.ContactPhone).HasMaxLength(50);
                Property(p => p.ContactPosition).HasMaxLength(50);
                Property(p => p.Contacts).HasMaxLength(50);
                Property(p => p.CreateTime);
                Property(p => p.CreditID).HasMaxLength(50);
                Property(p => p.IDCode).HasMaxLength(50);
                //Property(p => p.IsConfirm);
                Property(p => p.IsPersonal);
                Property(p => p.Accountant).HasMaxLength(50);
                HasOptional(p => p.ClientManager);
                HasOptional(p => p.District);
            }
        }

        #region ILogicalDelEntity 成员

        public bool IsDelete { get; set; }

        #endregion
    }

}
